# Python 2.6.4
# Project Euler, Problem 166
# Copyright 2010 Talha Zaman

from time import clock
start = clock()
count = 0
for i in xrange(10000):
    g00, g01, g02, g03 = i/1000, (i/100)%10, (i/10)%10, i%10
    tot = g00+g01+g02+g03
#    if tot!=2: continue
    for g10 in range(min(10, 1+tot-g00)):
        for g20 in range(min(10, 1+tot-g00-g10)):
            g30 = tot-g00-g10-g20
            if g30>9: continue
            for g13 in range(min(10, 1+tot-g03, 1+tot-g10)):
                for g23 in range(min(10, 1+tot-g03-g13, 1+tot-g20)):
                    g33 = tot-g03-g13-g23
                    if g33>9: continue
                    for g11 in range(min(10, 1+tot-g00-g33, 1+tot-g01, 1+tot-g10-g13)):
                        g12 = tot-g10-g11-g13
                        if g12>9 or g12<0: continue
                        g21 = tot-g03-g12-g30
                        if g21>9 or g21<0: continue
                        g22 = tot-g00-g11-g33
                        if g22>9 or g22<0: continue
                        if g20+g21+g22+g23!=tot: continue
                        g31 = tot-g01-g11-g21
                        if g31>9 or g31<0: continue
                        g32 = tot-g02-g12-g22
                        if g32>9 or g32<0: continue
                        if g30+g31+g32+g33!=tot: continue
#                        print g00, g01, g02, g03
#                        print g10, g11, g12, g13
#                        print g20, g21, g22, g23
#                        print g30, g31, g32, g33
#                        print
                        count += 1
#                    count += 1
stop = clock()
print count
print stop-start
